package com.jiangyg.mall.admin.controller;

import com.jiangyg.mall.admin.dto.DepartDTO;
import com.jiangyg.mall.admin.dto.DepartUserDTO;
import com.jiangyg.mall.admin.vo.DepartVO;
import com.jiangyg.mall.admin.vo.UserVO;
import com.jiangyg.mall.admin.service.DepartService;
import com.jiangyg.mall.core.support.restful.Pagination;
import com.jiangyg.mall.core.support.restful.Result;
import com.jiangyg.mall.core.utils.tree.Tree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 类描述：部门管理控制器
 *
 * @author jiangyg
 * @version 2.0
 * @date 2020-05-31
 */
@RestController
@RequestMapping("/depart")
public class DepartController {

    /**
     * 部门接口
     */
    private final DepartService departService;

    @Autowired
    public DepartController(DepartService departService) {
        this.departService = departService;
    }

    /**
     * 功能描述：获取部门树形结构
     *
     * @param request  请求
     * @param response 响应
     * @return 结果
     */
    @ResponseBody
    @GetMapping("/tree")
    public Result<List<Tree>> tree(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return Result.ok(departService.departTree());
    }

    /**
     * 功能描述：查询部门下的用户列表
     *
     * @param pagination 分页参数
     * @param departId   部门ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/user-list")
    public Result<Pagination<UserVO>> selectUserList(@RequestBody Pagination<UserVO> pagination,
                                                     @RequestParam("departId") Long departId) throws Exception {
        return Result.ok(departService.selectUserList(pagination, departId));
    }

    /**
     * 功能描述：查询未分配部门的用户列表
     *
     * @param request  请求
     * @param response 响应
     * @return 用户列表
     */
    @ResponseBody
    @PostMapping("/unallocated-user-list")
    public Result<List<UserVO>> selectUnallocatedUserList(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return Result.ok(departService.selectUnallocatedUserList());
    }

    /**
     * 功能描述：保存或者修改部门信息
     *
     * @param request 请求
     * @param dto     部门信息
     * @return 结果
     * @throws Exception 异常
     */
    @ResponseBody
    @PostMapping("/save")
    public Result<Object> saveOrUpdate(HttpServletRequest request, @RequestBody @Validated DepartDTO dto) throws Exception {
        return Result.saveOrUpdate(departService.saveOrUpdate(dto));
    }

    /**
     * 功能描述：保存部门和用户关联关系
     *
     * @param request 请求
     * @param dto     部门和用户关联关系
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/save-relation")
    public Result<Object> saveRelation(HttpServletRequest request, @RequestBody @Validated DepartUserDTO dto) {
        return Result.saveOrUpdate(departService.saveRelation(dto));
    }

    /**
     * 功能描述：删除部门和用户关联关系
     *
     * @param request 请求
     * @param dto     部门和用户关联关系
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/delete-relation")
    public Result<Object> deleteRelation(HttpServletRequest request, @RequestBody @Validated DepartUserDTO dto) {
        return Result.delete(departService.deleteRelation(dto));
    }

    /**
     * 功能描述：根据ID获取部门信息
     *
     * @param request  请求
     * @param departId 部门ID
     * @param details  详情标志，为 Y 的话，回显父级部门名称
     * @return 结果
     * @throws Exception 异常
     */
    @ResponseBody
    @PostMapping("/info")
    public Result<DepartVO> info(HttpServletRequest request,
                                 @RequestParam(name = "departId") Long departId,
                                 @RequestParam(name = "details", defaultValue = "N") String details) throws Exception {
        return Result.ok(departService.selectById(departId, details));
    }

    /**
     * 功能描述：删除部门信息
     *
     * @param request  请求
     * @param departId 批量删除的部门ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/delete")
    public Result<Object> delete(HttpServletRequest request, @RequestParam(name = "departId") Long departId) {
        return Result.delete(departService.delete(departId));
    }

}
